<style type="text/css">
    .f-title .am-selected {
        width: 80px;
    }
    .f-title .am-selected-btn {
        line-height: 28px;
        padding-top: 0px;
        padding-bottom: 0px;
    }
</style>

<div class="red" style="font-size: 14px; margin-bottom: 10px;">
    <div>【操作指引】在<bold>充值订单</bold>功能找到对应的订单，点击“<bold>标记成功</bold>”就可以。</div>
    <div>【惩罚扣款】例如要扣除用户100元，输入100即可，不要输入-100。</div>
</div>

<div class="form_content">
    <div class="f-title">
        <select class="code-phone">
            <option value="0">菠菜号</option>
            <option value="1">手机号</option>
        </select>
    </div>
    <div class="f-input">
        <input type="text" class="am-form-field username" placeholder="请输入用户菠菜号">
    </div>
    <div class="clear"></div>
    <div class="clue balance">
        积分当前余额：<span class="red balance">--</span> <br/>
        手机号：<span class="mobile red"></span><br>
        昵称：<span class="nick red"></span><br>
        头像：<img src="" class="icon" width="50px" height="50px"/>
    </div>

    <div class="f-title">
        操作类型
    </div>
    <div class="f-input">
        <div >
            <label class="am-radio-inline">
                <input type="radio" name="type" value="0" data-am-ucheck checked> 人工充值
            </label>
            <label class="am-radio-inline">
                <input type="radio" name="type" value="1" data-am-ucheck> 奖励发放
            </label>
            <label class="am-radio-inline">
                <input type="radio" name="type" value="2" data-am-ucheck> 惩罚扣款
            </label>
        </div>
    </div>
    <div class="clear"></div>
	
	<div class="f-title">
        金额
    </div>
    <div class="f-input">
        <input type="number" class="am-form-field amount" placeholder="请输入金额，元">
    </div>
    <div class="clear"></div>

    <div class="f-title">
        备注
    </div>
    <div class="f-input">
        <textarea placeholder="备注，最多60个文字" class="memo" maxlength="120" rows="5"></textarea>
    </div>
    <div class="clear"></div>

    <div class="button2">
        <button class="am-btn am-btn-primary btn-charge">提交</button>
    </div>
    <div class="clue">注意：提交成功后，请到“充值记录”或“扣款记录”中进行二次审核</div>
</div>

<script>
    var userinfo;
    function queryAccountInfo(user) {
        userinfo = user;
        $(".btn-charge").removeAttr("disabled");
        CMD.account_gquery_uid(user.mobile, function(r) {
            if (!r.list[0]) return;
            $(".balance .balance").html((r.list[0].balance/100).toFixed(2));
        });
        $(".balance .code").html(user.code);
        $(".balance .mobile").html(user.mobile || '');
        $(".balance .nick").html(user.nick || '');
        $(".balance .icon").attr("src", user.portrait || '');
    }

    function queryMemberInfo() {
        userinfo = null;
        $(".btn-charge").attr("disabled", true);
        $(".balance .red").html("--");

        var v = $(".username").val().trim();
        if (v == '') return;

        var v2 = $(".code-phone").val();
        if (v2 == 0) {
            // 菠菜号
            var code = parseInt(v);
            if (code) {
                CMD.find_user_by_code(code, function(r) {
                    if (Util.isEmpty(r)) {
                        alert2("用户" + code + "不存在！", true);
                    }
                    else {
                        queryAccountInfo(r);
                    }
                });
            }
        }
        else {
            CMD.find_user_by_uid(v, function(r) {
                if (Util.isEmpty(r)) {
                    alert2("用户" + v + "不存在！", true);
                    return;
                }
                queryAccountInfo(r);
            });
        }
    }

    if (urlInfo.code) {
        $(".username").val(urlInfo.code);
        queryMemberInfo();
    }
    $(".username").change(queryMemberInfo);

    $(".btn-charge").click(function() {
        if (!userinfo) return;
        var amount = $(".amount").val().trim();
        if (amount == '') {
            alert2("充值金额不对！", true);
            return;
        }
        amount = parseInt(amount);
        if (amount < 1) {
            alert2("充值金额至少为1！");
            return;
        }

        var type = $('input:radio:checked').val();
        if (type == 0) {
            // 人工充值
            startLoading();
            var memo = {
                info: $(".memo").val().trim(),
                time: new Date().Format("yyyy-MM-dd HH:mm:ss"),
                requestAccount: context.user.user.mobile,
            }
            CMD.deposit(userinfo.id, context.user.user.id, amount * 100, 
                "handOper", "manual", memo, function(r) {
                stopLoading();
                alert2("提交成功，请到“充值记录”中进行二次审核后，才能到账！");
            }, function() {
                stopLoading();
                alert2("充值失败！", true);
            });
            return;
        }
        if (type == 1) {
            // 奖励
            startLoading();
            CMD.reward(userinfo.id, context.user.user.id, amount * 100, $(".memo").val().trim(), function(r) {
                stopLoading();
                alert2("提交成功，请到“奖励记录”中进行二次审核后，才能到账！");
            }, function() {
                stopLoading();
                alert2("充值失败！", true);
            });
            return;
        }
        if (type == 2) {
            // 惩罚扣款
            startLoading();
            CMD.account_gpenalty(userinfo.id, context.user.user.id, amount * 100, $(".memo").val().trim(), function(r) {
                stopLoading();
                alert2("提交成功，请到“账变记录”中进行二次审核后，才能到账！");
            }, function() {
                stopLoading();
                alert2("扣款申请失败！", true);
            });
            return;
        }
    });

    $(".code-phone").change(function() {
        var v = $(".code-phone").val();
        $(".f-input .username").attr("placeholder", v == 0 ? "请输入用户菠菜号" : "请输入用户绑定的手机号");
        $(".f-input .username").val("");
    });
</script>
